/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataBase;

import Modelos.Orden;
import java.sql.ResultSet;

/**
 *
 * @author juanjo
 */
public class OrdenDB {
    
    public static void GuardaOrden(Orden orden){
       int id = orden.getIdCliente();
       int total = orden.getCarrito().getTotal();
       String columnas="(idCliente, total)";
       String valores="VALUES ("+id+","+total+");";
       String query="insert into orden "+columnas+" "+valores;
       BaseDeDatos.getConexion();
       BaseDeDatos.executeUpdate(query);
    }
    
    public static int GetIdOrden(){
        int id;
        String query = "select id from orden order by id desc limit 1;";
        BaseDeDatos.getConexion();
        
        ResultSet rs = BaseDeDatos.getResultSet(query);
        try{
            if(rs.next()){
                id = rs.getInt("id");
            }else{
                id=-1;
            }
        }catch(Exception e){
            id=-1;
        }
        return id;
    }
    
    public static Orden[] GetOrdenes(int idCliente){
         String query = "select * from orden where idCliente="+idCliente+";";
        int rows;
        int i=0;
        int idOrden, total;
        Orden[] orden;
        ResultSet rs = BaseDeDatos.getResultSet(query);
        try{
            if (rs.last()) {
                rows = rs.getRow();
                rs.beforeFirst();
            }else{
                return null;
            }
            orden = new Orden[rows];
            while(rs.next()){
                idOrden = rs.getInt("id");
                total = rs.getInt("total");
                orden[i]= new Orden(idOrden, idCliente, total);
                i++;
            }
            
        }catch(Exception e){
            orden = null;
        }
        
        return orden;
    }
}
